package org.gwt.pluginframework.plugin;

import org.gwt.pluginframework.common.IPlugin;
import org.gwt.pluginframework.common.Plugin;
import org.gwt.pluginframework.common.event.IPluginEvent;
import org.gwt.pluginframework.common.event.PluginEvent;
import org.gwt.pluginframework.common.event.PluginEventHandler;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.google.gwt.user.client.ui.Widget;

public class Navigation extends Plugin implements PerspectiveWestExtension {
 
	private static final long serialVersionUID = 45100345208855381L;

	private ContentPanel rootPanel;
	
	protected TreePanel<ModelData> tree;
	protected TreeStore<ModelData> store;
	
	protected IPlugin plugin;

	public void setupManifest() {
		manifest.setName("Navigation");
		manifest.setVersion("1.0");
	}

	@Override
	public void onInitialize() {
		if (rootPanel != null) {
			throw new IllegalStateException(
					"NavigationPanel was already loaded!");
		}
		rootPanel = new ContentPanel();
		rootPanel.setHeading("Navigation");
		
		registerEventHandler(new PluginEventHandler(CustomEvents.FOCUS_ITEM_EVENT) {
			public void handle(IPluginEvent event) {
				tree.getSelectionModel().select(false, store.getRootItems().get(0)); 				
			}
		});
	}

	@Override
	public void onStart() {
		store = new TreeStore<ModelData>();  
		
		tree = new TreePanel<ModelData>(store);
		tree.setCheckable(false);
		tree.setSelectionModel(new TreePanelSelectionModel<ModelData>());
		tree.getSelectionModel().select(store.getRootItems(), true);  

		ModelData formA = new BaseModelData();
		formA.set("name", "FormA");
		store.add(formA, false);
		
		TreeModel formB = new BaseTreeModel();
		formB.set("name", "FormB");

		TreeModel first = new BaseTreeModel();
		first.set("name", "First");
		formB.add(first);
		
		TreeModel second = new BaseTreeModel();
		second.set("name", "Second");
		
		ModelData firstHalf = new BaseModelData();
		firstHalf.set("name", "Second starting half");
		second.add(firstHalf);
		
		ModelData secondHalf = new BaseModelData();
		secondHalf.set("name", "Second ending half");
		second.add(secondHalf);
		formB.add(second);
		
		ModelData third = new BaseModelData();
		third.set("name", "Third");
		formB.add(third);

		store.add(formB, true);

		ModelData formC = new BaseModelData();
		formC.set("name", "FormC");
		store.add(formC, false);
	
		tree.addListener(Events.OnClick, new Listener<TreePanelEvent<ModelData>>() {
			public void handleEvent(TreePanelEvent<ModelData> be) {
				String name = be.getItem().get("name");
				if("FormA".equals(name)) {
					manager.dispatch(new PluginEvent(
						CustomEvents.START_FORM_A_EVENT, Navigation.this));
				} else if("FormB".equals(name)) {
					manager.dispatch(new PluginEvent(
						CustomEvents.START_FORM_B_EVENT, Navigation.this));
				} else if("FormC".equals(name)) {
					manager.dispatch(new PluginEvent(
						"createFormCPluginEvent", Navigation.this));
				}
			}
		});

		rootPanel.add(tree);
		rootPanel.layout();
	}

	public int getOrder() {
		return 0;
	}

	public Widget getWidget() {
		return rootPanel;
	}

	public void setParent(IPlugin plugin) {
		this.plugin = plugin;
	}

	public IPlugin getPlugin() {
		return plugin;
	}
}
